﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DNA
{
    public partial class regres : Form
    {
        public Int32 numberofequel;
        int where_y = 0;


        public regres()
        {
            InitializeComponent();

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        public void fillcomuns()
        {
            colums.Items.Clear();
            for (int j = 0; j < colums.Items.Count; j++)
            {
                colums.Items.RemoveAt(j);
            }
            DNA dna = this.Owner as DNA;
            if (dna.flag == true)
            {
                for (int i = 0; i < dna.TDNA.Columns.Count; i++)
                {
                    colums.Items.Add(dna.TDNA.Columns[i].HeaderText);
                    colums.SetItemChecked(i, true);
                    y.Items.Add(dna.TDNA.Columns[i].HeaderText);
                    y.SetItemChecked(i, false);
                }
            }
            eq_count.Maximum = dna.TDNA.Rows.Count-1;
            eq_count.Value = eq_count.Maximum;
        }

        private void whetre_my_y()
        {
            for (int i = 0; i < y.Items.Count; i++)
            {
                if (y.GetItemChecked(i) == true)
                {
                    where_y = i;
                    
                }
                else
                { continue; }
            }
        }

        private void summ_y()
        {
            float summ1 = 0;
            DNA dna = this.Owner as DNA;
            for (int i = 0; i < numberofequel; i++)
            {
                summ1 = summ1 + Convert.ToInt32(dna.TDNA[where_y, i].Value);
            }
           // regres_tab.Rows.Add('y', summ1);
            summ1 = summ1/(numberofequel);
            regres_sred.Rows.Add('y', summ1);
        }


        private void summ_x()
        {
            string name;
            double summ1 = 0;
           // double summ2 = 0;
            DNA dna = this.Owner as DNA;
            for (int i = 0; i < colums.Items.Count; i++)
            {
                if (colums.GetItemChecked(i) == true)
                {
                    for (int j = 0; j < numberofequel; j++)
                    {
                        summ1 = summ1 + Convert.ToInt32(dna.TDNA[i, j].Value);
                      //  summ2 = Math.Pow(summ1, 2);
                    }
                    name = dna.TDNA.Columns[i].HeaderText;
                   // regres_tab.Rows.Add(name, summ1);
                    summ1 = summ1 / (numberofequel );
                    regres_sred.Rows.Add(name, summ1);
                   // name = name + "^2";
                  //  regres_tab.Rows.Add(name, summ2);
                    name="";
                    summ1 = 0;
                }
                else { continue; }
            }

        }

        private void summ_x_x()
        {
            string name;
            double summ1 = 0;
            DNA dna = this.Owner as DNA;
            for (int i = 0; i < colums.Items.Count; i++)
            {
                if (colums.GetItemChecked(i) == true)
                {
                    name = dna.TDNA.Columns[i].HeaderText+"*";
                    for (int j = i + 1; j < colums.Items.Count; j++)
                    {
                        if (colums.GetItemChecked(j) == true)
                        {

                            for (int row = 0; row < numberofequel - 1; row++)
                            {
                                summ1 = summ1 + Convert.ToInt32(dna.TDNA[i, row].Value) * Convert.ToInt32(dna.TDNA[j, row].Value);
                            }
                            regres_tab.Rows.Add(name + dna.TDNA.Columns[j].HeaderText, summ1);
                            summ1 = 0;
                        }
                    }
                }
            }
        
        }

        private void shumel_method()
        {
            double summ1 = 0;
            double summ2 = 0;
            double sred_x = 0;
            Int32 where_x=0;
            string name_x;
            double sred_y = Convert.ToDouble(regres_sred[1, 0].Value);
            DNA dna = this.Owner as DNA;
            try
            {
                for (int i = 1; i < regres_sred.Rows.Count - 1; i++)
                {
                    sred_x = Convert.ToDouble(regres_sred[1, i].Value);
                    name_x = Convert.ToString(regres_sred[0, i].Value);
                    for (int z = 0; z < dna.TDNA.ColumnCount; z++)
                    {
                        if (name_x == dna.TDNA.Columns[z].HeaderText)
                        {
                            where_x = z;
                        }
                    }
                    for (int j = 0; j < numberofequel; j++)
                    {
                        summ1 = summ1 + ((Convert.ToDouble(dna.TDNA[where_x, j].Value) - sred_x) * ((Convert.ToDouble(dna.TDNA[where_y, j].Value)) - sred_y));
                        summ2 = summ2 + Math.Pow((Convert.ToDouble(dna.TDNA[where_x, j].Value) - sred_x), 2);
                    }
                    summ1 = summ1 / summ2;
                    text_proces.Text = text_proces.Text + "Значение коэффициента " + name_x + " : " + Convert.ToString(summ1) + '\n';
                    summ1 = 0;
                    summ2 = 0;
                }
            }
            catch
            {
                MessageBox.Show("Выберете числовые переменные!");
            }
            
        }

        private void start_anal_Click(object sender, EventArgs e)
        {
            int Y = -1;
            int count=0;
            numberofequel = Convert.ToInt32(eq_count.Value);
            for (int i = 0; i < y.Items.Count; i++)
            {
                if (y.GetItemChecked(i) == true)
                {
                    Y = i;
                    break;
                }
            }
            if (Y == -1) MessageBox.Show('\a' + "Выберете зависимую переменную!");
            else
            {
                for (int i = 0; i < colums.Items.Count; i++)
                {
                    if (colums.GetItemChecked(i) == true) count++;
                    if (count > 2) break;
                }
                if (count <= 2) MessageBox.Show('\a' + "Выберете более двух атрибутов!");
                else start_analis();
            }
        }

        private void start_analis()
        {
            DNA dna = this.Owner as DNA;
            int test;
            for (int i = 0; i < colums.Items.Count; i++)
            {
                if (colums.GetItemChecked(i) == true)
                {
                    for (int j = 0; j < 1; j++)
                    {
                        try
                        {
                            test = Convert.ToInt32(dna.TDNA[i, j].Value);
                        }
                        catch
                        {
                            colums.SetItemChecked(i, false);
                        }
                    }
                    if (colums.Items[i].ToString() == "Диагноз")
                    {
                        colums.SetItemChecked(i, false);
                    }
                }  
            }
            regres_tab.Rows.Clear();
            regres_sred.Rows.Clear();
            text_proces.Text = "==========================================================================" + '\n';
            text_proces.Text = text_proces.Text + "Идёт расчёт параметров" + '\n';
            whetre_my_y();
            summ_y();
            summ_x();
          //  summ_x_x();
            text_proces.Text = text_proces.Text + "==========================================================================" + '\n';
            text_proces.Text = text_proces.Text + "Расчёт завершён.Идёт составление уравнений" + '\n';
            shumel_method();
        }

        private void colums_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            { start_analis(); }
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }

 }

